package com.weetop.cfw.base.presenter.imp

import com.weetop.cfw.base.presenter.CloudCurriculumBrowseRecordPresenter
import com.weetop.cfw.base.view.BaseView
import com.weetop.cfw.base.view.CloudCurriculumBrowseRecordView
import com.weetop.cfw.bean.AudioVideoBrowsingRecordBean
import com.weetop.cfw.bean.ErrorBean
import com.weetop.cfw.callback.RxJavaCallBack
import com.weetop.cfw.mine.activity.CloudCurriculumBrowseRecordActivity
import com.weetop.cfw.utils.RetrofitUtils
import com.weetop.cfw.utils.RxJavaUtils

class CloudCurriculumBrowseRecordPresenterImp(var cloudCurriculumBrowseRecordView: CloudCurriculumBrowseRecordView? = null): CloudCurriculumBrowseRecordPresenter {

    /**
     * 获取云课程浏览记录
     * @param curpage 当前数据的页数
     */
    override fun getVideoBrowsingRecord(cloudCurriculumBrowseRecordActivity: CloudCurriculumBrowseRecordActivity, curpage: Int) {
        cloudCurriculumBrowseRecordActivity.showLoadingDialog(cloudCurriculumBrowseRecordActivity,"正在加载云课程浏览记录...")
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().getVideoBrowsingRecord(curpage = curpage),cloudCurriculumBrowseRecordActivity,object:RxJavaCallBack<AudioVideoBrowsingRecordBean>(){
            override fun onSuccess(t: AudioVideoBrowsingRecordBean?) {
                cloudCurriculumBrowseRecordActivity.dismissLoadingDialog()
                cloudCurriculumBrowseRecordView?.getVideoBrowsingRecordSuccess(t)
            }
            override fun onRequestError(t: AudioVideoBrowsingRecordBean?) {
                super.onRequestError(t)
                cloudCurriculumBrowseRecordActivity.dismissLoadingDialog()
                cloudCurriculumBrowseRecordView?.getVideoBrowsingRecordFailed()
            }

            override fun onErrorResponse(throwable: Throwable?) {
                super.onErrorResponse(throwable)
                cloudCurriculumBrowseRecordActivity.dismissLoadingDialog()
                cloudCurriculumBrowseRecordView?.getVideoBrowsingRecordFailed()
            }

        })
    }

    /**
     * 清空商业云课浏览记录
     */
    override fun clearCloudCurriculumBrowseRecord(cloudCurriculumBrowseRecordActivity: CloudCurriculumBrowseRecordActivity) {
        cloudCurriculumBrowseRecordActivity.showLoadingDialog(cloudCurriculumBrowseRecordActivity,"正在清空商业云课浏览记录...")
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().clearBrowseRecordList(2),cloudCurriculumBrowseRecordActivity,object:RxJavaCallBack<ErrorBean>(){
            override fun onSuccess(t: ErrorBean?) {
                cloudCurriculumBrowseRecordActivity.dismissLoadingDialog()
                cloudCurriculumBrowseRecordActivity.showNativeShortToast("商业云课浏览记录清除成功")
                cloudCurriculumBrowseRecordView?.clearCloudCurriculumBrowseRecordSuccess()
            }

            override fun onRequestError(t: ErrorBean?) {
                super.onRequestError(t)
                cloudCurriculumBrowseRecordActivity.dismissLoadingDialog()
                cloudCurriculumBrowseRecordActivity.showNativeShortToast(t?.message)
            }

            override fun onErrorResponse(throwable: Throwable?) {
                super.onErrorResponse(throwable)
                cloudCurriculumBrowseRecordActivity.dismissLoadingDialog()
            }

        })
    }



    /**
     * 绑定
     */
    override fun attachView(baseView: BaseView) {
        cloudCurriculumBrowseRecordView = baseView as? CloudCurriculumBrowseRecordView
    }

    /**
     * 解绑
     */
    override fun detachView() {
        cloudCurriculumBrowseRecordView = null
    }
}